local Screen = require "widgets/screen"
local Image  = require "widgets/image"
local Text = require "widgets/text"
local ImageButton = require "widgets/imagebutton"
local TEMPLATES = require "widgets/redux/templates"

local PopupDialogScreen = Class(Screen, function(self, title , content ,params_window,num_of_buttons,params_button)
    Screen._ctor(self, "PopupDialogScreen")

    self.root = self:AddChild(TEMPLATES.ScreenRoot())
    self.bg = self.root:AddChild(TEMPLATES.BackgroundTint(0.7))

    self.window = self.root:AddChild(Image(params_window.atlas or "images/my_futuristic_ui/my_futuristic_ui.xml", params_window.tex or "name_preset_bg.tex"))
    local scale = params_window.scale or {0.7, 0.7}
    self.window:SetScale(unpack(scale))

    self.title = self.window:AddChild(Text( NEWFONT_OUTLINE,  70, title))
    self.title:SetPosition(0, 100)

    self.content = self.window:AddChild(Text(NEWFONT_OUTLINE, 50, content))
    self.content:SetPosition(0, 0)
    self.content:SetRegionSize(700, 200)
    self.content:EnableWordWrap(true)
    self.content:SetHAlign(ANCHOR_MIDDLE)

    if num_of_buttons == 1 then
        -- Cancel button
        local cancel_button = ImageButton(
        params_button.cancel.atlas or "images/my_futuristic_ui/my_futuristic_ui.xml",
        params_button.cancel.normal or "preset_button_normal.tex",
        params_button.cancel.hover or "preset_button_hover.tex",
        params_button.cancel.disabled or "preset_button_disabled.tex",
        params_button.cancel.down or "preset_button_down.tex")
        cancel_button:SetText(params_button.cancel.text or STRINGS.UI.CUSTOMIZATIONSCREEN.BACK)
        local pos = params_button.cancel.position or {0, -90}
        cancel_button:SetPosition(unpack(pos))
        local scale = params_button.cancel.scale or {0.5, 0.5}
        cancel_button:SetScale(unpack(scale))
        local text_scale = params_button.cancel.text_scale or {1, 1}
        cancel_button.text:SetScale(unpack(text_scale))
        cancel_button:SetOnClick(function() 
            if params_button.cancel.cb then
                params_button.cancel.cb()
            end
            TheFrontEnd:PopScreen()
        end)
        self.window:AddChild(cancel_button)
    elseif num_of_buttons == 2 then
        -- Cancel button
        local cancel_button = ImageButton(
        params_button.cancel.atlas or "images/my_futuristic_ui/my_futuristic_ui.xml",
        params_button.cancel.normal or "preset_button_normal.tex",
        params_button.cancel.hover or "preset_button_hover.tex",
        params_button.cancel.disabled or "preset_button_disabled.tex",
        params_button.cancel.down or "preset_button_down.tex")
        cancel_button:SetText(params_button.cancel.text or STRINGS.UI.CUSTOMIZATIONSCREEN.BACK)
        local pos = params_button.cancel.position or {-170, -90}
        cancel_button:SetPosition(unpack(pos))
        local scale = params_button.cancel.scale or {0.5, 0.5}
        cancel_button:SetScale(unpack(scale))
        local text_scale = params_button.cancel.text_scale or {1, 1}
        cancel_button.text:SetScale(unpack(text_scale))
        cancel_button:SetOnClick(function() 
            if params_button.cancel.cb then
                params_button.cancel.cb()
            end
            TheFrontEnd:PopScreen()
        end)
        self.window:AddChild(cancel_button)

        -- Confirm button
        local confirm_button = ImageButton(
        params_button.confirm.atlas or "images/my_futuristic_ui/my_futuristic_ui.xml",
        params_button.confirm.normal or "preset_button_normal.tex",
        params_button.confirm.hover or "preset_button_hover.tex",
        params_button.confirm.disabled or "preset_button_disabled.tex",
        params_button.confirm.down or "preset_button_down.tex")
        confirm_button:SetText(params_button.confirm.text or STRINGS.UI.CUSTOMIZATIONSCREEN.CONFIRM)
        local pos =  params_button.confirm.position or {170, -90}
        confirm_button:SetPosition(unpack(pos))
        local scale = params_button.confirm.scale or {0.5, 0.5}
        confirm_button:SetScale(unpack(scale))
        local text_scale = params_button.confirm.text_scale or {1, 1}
        confirm_button.text:SetScale(unpack(text_scale))
        confirm_button:SetOnClick(function()
            if params_button.confirm.cb then
                params_button.confirm.cb()
            end
            TheFrontEnd:PopScreen()
        end)
        self.window:AddChild(confirm_button)
    end

end)

return PopupDialogScreen